// export const audioContext = wx.createInnerAudioContext()
// 替代
export const audioContext = wx.getBackgroundAudioManager()

import {
    HYEventStore
} from 'hy-event-store'
import {
    getSongDetail,
    getSongLyric
} from '../service/api_player'

import {
    parseLyric
} from '../utils/parse-lyric'

export const playerStore = new HYEventStore({
    state: {
        id: 0,
        currentSong: {},
        lyricInfo: [],
        durationTime: 0,

        currentTime: 0, // 歌曲当前播放时间
        currentLyric: '', // 当前歌词
        currentIndex: 0,

        playModeIndex: 0, //播放模式
        isPlaying: false, //是否正在播放

        playListSongs: [], //歌曲列表
        playListIndex: 0 // 歌曲索引
    },
    actions: {
        playMusicWithSongIdAction(state, {
            id
        }) {
            if (state.id === id) return
            state.id = id
            state.isPlaying = true // 修改播放状态
            state.currentSong = {}
            state.lyricInfo = []
            state.durationTime = 0
            state.currentIndex = 0
            state.currentLyric = ''
            // 歌曲详情
            getSongDetail(id).then(res => {
                state.currentSong = res.songs[0],
                state.durationTime = res.songs[0].dt,
                audioContext.title = res.songs[0].name
            })
            getSongLyric(id).then(res => {
                const lyricString = res.lrc.lyric
                // 获取到解析之后的歌词
                const lyric = parseLyric(lyricString)
                state.lyricInfo = lyric
            })

            // 播放对应id歌曲
            audioContext.stop() //播放音乐之前停止掉之前的音乐
            audioContext.src = `https://music.163.com/song/media/outer/url?id=${id}.mp3`
            audioContext.title = id
            // 音乐准备好之后开始播放
            audioContext.autoplay = true

            //  
            this.dispatch('setupAudioContextLisnnerAction')

        },
        setupAudioContextLisnnerAction(state) {
            audioContext.onCanplay(() => {
                audioContext.play()
            })
            audioContext.onTimeUpdate(() => {
                const currentTime = audioContext.currentTime * 1000
                state.currentTime = currentTime
                if (!state.lyricInfo.length) return
                for (let i = 0; i < state.lyricInfo.length; i++) {
                    if (state.lyricInfo[i].time > currentTime) {
                        // 找到当前播放的歌曲之后跳出循环
                        if (state.currentIndex !== i) { //避免多次重复设置相同歌词
                            state.currentLyric = state.lyricInfo[i - 1].lyricText,
                                state.currentIndex = i
                        }
                        break
                    }
                }
            })
            // 歌曲播放完毕
            audioContext.onEnded(()=>{
                this.dispatch('changeNewMusicAction',true)
            })
            // 监听音乐暂停/播放
            audioContext.onPlay(()=>{
                state.isPlaying = true
            })
            audioContext.onPause(()=>{
                state.isPlaying = false
            })
            audioContext.stop(()=>{
                state.isPlaying = false
            })
        },
        changeMusicPlayStatusAction(state, isPlay) {
            // 播放暂停action
            state.isPlaying = isPlay
            state.isPlaying ? audioContext.play() : audioContext.pause()
        },
        changeNewMusicAction(state,{isNext=true}) {
            // 获取当前索引
            let index = state.playListIndex
            // 播放模式
            const playModeIndex = state.playModeIndex

            switch (playModeIndex) {
                case 0:
                    index = isNext? index + 1 :index - 1
                    if(index === -1 ) index = state.playListSongs.length - 1
                    if (index === state.playListSongs.length) index = 0;
                    break;
                case 1 :
                     break;
                case 2 :
                    console.log('dd');
                    index = Math.floor(Math.random() * state.playListSongs.length)
                    break;
            }
            state.playListIndex = index
            // 获取歌曲
            let currentSong = state.playListSongs[index]
            if(!currentSong) currentSong = state.currentSong

            this.dispatch('playMusicWithSongIdAction',{id:currentSong.id})
        }
    }
})